package com.geeker.module.sys.user.sevice;

import cn.hutool.crypto.digest.DigestUtil;
import com.geeker.common.api.CommonResult;
import com.geeker.common.exception.BusinessException;
import com.geeker.common.core.service.TokenService;
import com.geeker.common.core.domain.AccessUser;
import com.geeker.module.sys.user.domain.SysUser;
import com.geeker.module.sys.user.dto.UpdatePwdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysProfileService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysUserService sysUserService;

    public CommonResult updatePwd(UpdatePwdDto dto) {
        AccessUser accessUser = tokenService.getAccessUser();
        SysUser sysUser = sysUserService.getById(accessUser.getUserId());
        if (!DigestUtil.bcryptCheck(dto.getOldPassword(), sysUser.getPassword())) {
            throw new BusinessException("旧密码错误");
        }
        if (DigestUtil.bcryptCheck(dto.getNewPassword(), sysUser.getPassword())) {
            throw new BusinessException("新密码不能与旧密码相同");
        }
        sysUser.setPassword(dto.getNewPassword());
        sysUserService.updateById(sysUser);
        return CommonResult.success();
    }
}
